﻿#include "Guard.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool Guard::getBlocking() const
    {
        return true;
    }

    bool Guard::getCanMove() const
    {
        return Route.size() > 0 && !Dead && getEnvir()->getTime() > MoveTime && getEnvir()->getTime() > ActionTime && getEnvir()->getTime() > ShockTime;
    }

    bool Guard::getCanRegen() const
    {
        return false;
    }

    Guard::Guard(MonsterInfo *info) : MonsterObject(info)
    {
        NameColour = Color::SkyBlue;
    }

    void Guard::Spawned()
    {
        if (Respawn != nullptr && Respawn->Info->Direction < 8)
        {
            setDirection(static_cast<MirDirection>(Respawn->Info->Direction));
        }

        MonsterObject::Spawned();
    }

    bool Guard::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }

        return getTarget()->getCurrentLocation() != getCurrentLocation() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), Info->ViewRange);
    }

    void Guard::ProcessRegen()
    {
    }

    void Guard::ProcessRoam()
    {
        if (getCanMove())
        {
            MonsterObject::ProcessRoam();
        }
    }

    bool Guard::IsAttackTarget(PlayerObject *attacker)
    {
        return false;
    }

    bool Guard::IsAttackTarget(MonsterObject *attacker)
    {
        return false;
    }

    int Guard::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        throw NotSupportedException();
    }

    int Guard::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        throw NotSupportedException();
    }

    int Guard::Struck(int damage, DefenceType type)
    {
        return 0;
    }

    bool Guard::DropItem(UserItem *item)
    {
        throw NotSupportedException();
    }

    bool Guard::DropGold(unsigned int gold)
    {
        throw NotSupportedException();
    }

    void Guard::Die()
    {
    }

    void Guard::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            return;
        }

        Point *target = getTarget()->getBack();
        MirDirection dir = Functions::DirectionFromPoint(target, getTarget()->getCurrentLocation());

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = dir;
        tempVar->Location = target;
        Broadcast(tempVar);
        S::ObjectTurn *tempVar2 = new S::ObjectTurn();
        tempVar2->ObjectID = ObjectID;
        tempVar2->Direction = getDirection();
        tempVar2->Location = getCurrentLocation();
        Broadcast(tempVar2);

        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

        if (getTarget()->getRace() != ObjectType::Player)
        {
            damage = std::numeric_limits<int>::max();
        }

        if (damage == 0)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::AC});
        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }
}
